<template>
  <!-- 音乐详情、播放页 -->
  <div class="LyricDetail">
    <div
      class="bg"
      :style="{ backgroundImage: `url(${detail.al.picUrl})` }"
    ></div>
    <div class="playTop">
      <div class="back" @click="$emit('back')">
        <span class="iconfont icon-zuojiantou"></span>
      </div>
      <div class="center">
        <div class="title">
          <marquee behavior="scroll" direction="left">
            {{ detail.name }}
          </marquee>
        </div>
      </div>
      <div class="share">
        <span class="iconfont icon-fenxiang"></span>
      </div>
    </div>

    <!-- 歌词和唱片盘切换 -->
    <div class="playContent" v-if="!isLyric">
      <img
        src="@/assets/img/needle.png"
        class="needle"
        :class="{ active: isPlay }"
      />
      <img src="@/assets/img/disc_round.png" class="disc" />
      <img :src="detail.al.picUrl" alt="" class="pic" />
    </div>
    <div class="playLyric" v-else ref="playLyric">
      <p
        v-for="(item, i) in $store.getters.lyricList"
        :key="i"
        :class="{
          active:
            currentTime * 1000 > item.time && currentTime * 1000 < item.next,
        }"
      >
        {{ item.lyric }}
      </p>
    </div>

    <div class="progress"></div>
    <div class="playFooter">
      <span class="iconfont icon-xunhuan"></span>
      <span
        class="iconfont icon-shangyishoushangyige"
        @click="goPlay(-1)"
      ></span>
      <span
        class="iconfont icon-bofang"
        v-if="!isPlay"
        @click="
          play();
          startRun = false;
        "
      ></span>
      <span
        class="iconfont icon-zanting"
        v-else
        @click="
          pause();
          clearTimer();
        "
      ></span>
      <span class="iconfont icon-xiayigexiayishou" @click="goPlay(1)"></span>
      <span class="iconfont icon-24gl-playlistMusic"></span>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
var timer = null;
export default {
  data() {
    return {
      isLyric: true,
      startRun: false,
    };
  },
  props: ["detail", "isPlay", "play", "pause"],
  computed: {
    ...mapState(["playList", "playCurrentIndex", "currentTime"]),
  },
  watch: {
    // 监听时间从而改变歌词展示位置
    currentTime(newVal) {
      let curP = document.querySelector("p.active");
      if (curP) {
        let offsetTop = curP.offsetTop;

        if (this.$refs.playLyric.scrollTop < offsetTop) {
          console.log("还没到");
          if (!this.startRun) {
            this.startRun = true;
            timer = setInterval(() => {
              this.$refs.playLyric.scrollTop++;
            }, 30);
          }
        } else {
          console.log("到了");
          clearInterval(timer);
          timer = null;
          this.startRun = false;
        }
      }
    },
  },
  methods: {
    // 上一首、下一首
    goPlay(num) {
      let index = this.playCurrentIndex + num;
      if (index < 0) {
        index = this.playList.length - 1;
      } else if (index == this.playList.length) {
        index = 0;
      }
      this.$store.commit("setPlayIndex", index);
    },
    clearTimer() {
      console.log("暂停");
      clearInterval(timer);
      timer = null;
      this.startRun = true;
    },
  },
};
</script>
<style lang="less" scoped>
.LyricDetail {
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  background-color: #fff;
  .bg {
    position: absolute;
    left: 0;
    top: 0;
    width: 100vw;
    height: 100vh;
    background-size: auto 100%;
    background-position: center;
    filter: blur(60px);
  }
  .playTop {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 7.5rem;
    height: 1.2rem;
    position: absolute;
    left: 0;
    top: 0;
    color: #fff;
    padding: 0 0.4rem;
    .iconfont {
      font-size: 0.5rem;
    }
    marquee {
      width: 5rem;
      font-size: 0.4rem;
    }
  }
  .playContent {
    position: absolute;
    width: 7.5rem;
    height: 3rem;
    left: 0;
    top: 1.5rem;
    .needle {
      width: 1.6rem;
      height: auto;
      position: absolute;
      left: 50%;
      z-index: 2;
      transition: all 1s;
      transform-origin: 0.3rem 0;
      transform: rotate(-10deg);
    }
    .needle.active {
      transform: rotate(30deg);
      transform-origin: 0.3rem 0;
      transition: all 1s;
    }
    .disc {
      width: 4.5rem;
      position: absolute;
      left: 50%;
      top: 2rem;
      transform: translate(-50%);
      z-index: 1;
    }
    .pic {
      width: 3rem;
      height: 3rem;
      border-radius: 50%;
      position: absolute;
      left: 50%;
      top: 2.8rem;
      transform: translate(-50%);
    }
  }
  .playLyric {
    position: absolute;
    width: 7.5rem;
    height: 8.8rem;
    line-height: 0.88rem;
    overflow: scroll;
    left: 0;
    top: 1.5rem;
    text-align: center;
    color: #fff;
    .active {
      color: #fee082;
      font-size: 0.4rem;
    }
  }
  .playFooter {
    width: 7.5rem;
    height: 1.3rem;
    position: absolute;
    left: 0;
    bottom: 0;
    display: flex;
    justify-content: space-between;
    align-content: center;
    padding: 0 0.4rem;
    .iconfont {
      font-size: 0.5rem;
      color: #fff;
      line-height: 1.3rem;
    }
    .icon-bofang {
      font-size: 0.96rem;
    }
    .icon-zanting {
      font-size: 0.9rem;
    }
  }
}
</style>